Ext.define('YX.core.CommonDataSetup', {
	 extend : 'Ext.panel.Panel',
	 layout : 'border',
	 gridDef : null,
	 btnAddDtl : null,
	 btnSaveDtl : null,
	 btnDeleteDtl : null,
	 gridDtl : null,
	 txtSearchGroupName : null,
	 txtSearchGroupDesc : null,
	 frmSearch : null,
	 txtGroupName : null,
	 txtGroupDesc : null,
	 txtKey1 : null,
	 txtKey2 : null,
	 txtKey3 : null,
	 txtKey4 : null,
	 txtKey5 : null,
	 txtData1 : null,
	 txtData2 : null,
	 txtData3 : null,
	 txtData4 : null,
	 txtData5 : null,
	 frmOperater : null,
	 btnSaveDef : null,
	 btnDeleteDef : null,
	 btnSearchDef : null,
	 btnResetDef : null,
	 initComponent : function() {
	  //通用数据定义清单
	  this.gridDef = Ext.create('Ext.grid.Panel', {
		   title : T.label.definition,
		   flex : 1,
		   columns : [{
		      text : T.label.group,
		      dataIndex : 'groupName'
		     }, {
		      text : T.label.desc,
		      dataIndex : 'groupDesc'
		     }, {
		      text : T.label.key + "1",
		      dataIndex : 'key1'
		     }, {
		      text : T.label.key + "2",
		      dataIndex : 'key2'
		     }, {
		      text : T.label.key + "3",
		      dataIndex : 'key3'
		     }, {
		      text : T.label.key + "4",
		      dataIndex : 'key4'
		     }, {
		      text : T.label.key + "5",
		      dataIndex : 'key5'
		     }, {
		      text : T.label.value + "1",
		      dataIndex : 'data1'
		     }, {
		      text : T.label.value + "2",
		      dataIndex : 'data2'
		     }, {
		      text : T.label.value + "3",
		      dataIndex : 'data3'
		     }, {
		      text : T.label.value + "4",
		      dataIndex : 'data4'
		     }, {
		      text : T.label.value + "5",
		      dataIndex : 'data5'
		     }, {
		      text : T.label.createTime,
		      dataIndex : 'createDate',
		      xtype : 'datecolumn',
		      format : 'Y-m-d H:i:s',
		      width : 130
		     }, {
		      text : T.label.createUser,
		      dataIndex : 'createUserId'
		     }, {
		      text : T.label.updateTime,
		      dataIndex : 'updateDate',
		      xtype : 'datecolumn',
		      format : 'Y-m-d H:i:s',
		      width : 130
		     }, {
		      text : T.label.updateUser,
		      dataIndex : 'updateUserId'
		     }],
		   store : {
		    xtype : 'json',
		    pageSize : 10,
		    fields : ['orgCode', 'groupName', 'groupDesc', 'key1', 'key2', 'key3', 'key4', 'key5', 'data1', 'data2',
		      'data3', 'data4', 'data5', {
			     type : 'date',
			     name : 'createDate',
			     dateFormat : 'YmdHis'
		      }, 'createUserId', {
			     type : 'date',
			     name : 'updateDate',
			     dateFormat : 'YmdHis'
		      }, 'updateUserId'],
		    proxy : {
			   type : 'zbusrpc',
			   module : 'BasViewCommonDataSetupDefinitionService',
			   reader : {
			    root : 'cdsDefList'
			   }
		    }
		   },
		   listeners : {
		    scope : this,
		    select : this.onGridDefSelect
		   }
	    });

	  //明细操作按钮
	  this.btnAddDtl = Ext.create('Ext.button.Button', {
		   iconCls : 'icon-add',
		   text : T.label.add,
		   scope : this,
		   handler : this.onBtnAddDtlClicked
	    });
	  this.btnSaveDtl = Ext.create('Ext.button.Button', {
		   iconCls : 'icon-disk',
		   itemId : 'btnSaveDtl',
		   text : T.label.save,
		   scope : this,
		   handler : this.onBtnSaveDtlClicked
	    });
	  this.btnDeleteDtl = Ext.create('Ext.button.Button', {
		   iconCls : 'icon-remove',
		   text : T.label.remove,
		   scope : this,
		   handler : this.onBtnDeleteDtlClicked
	    });
	  //明细列表
	  this.gridDtl = Ext.create('Ext.grid.Panel', {
		   title : T.label.detail,
		   flex : 1,
		   enableColumnHide : false,
		   selModel : Ext.create('Ext.selection.CellModel'),
		   plugins : [{
		      ptype : 'cellediting',
		      clicksToEdit : 1
		     }],
		   tbar : {
		    items : [this.btnAddDtl, this.btnDeleteDtl, '-', this.btnSaveDtl]
		   },
		   columns : [{
		      text : T.label.group,
		      dataIndex : 'groupName'
		     }, {
		      dataIndex : 'key1',
		      hidden : true,
		      editor : Ext.create('Ext.form.field.Text', {
			       xtype : 'textfield',
			       allowBlank : false,
			       maxLength : 50
		        })
		     }, {
		      dataIndex : 'key2',
		      hidden : true,
		      editor : Ext.create('Ext.form.field.Text', {
			       xtype : 'textfield',
			       maxLength : 50
		        })
		     }, {
		      dataIndex : 'key3',
		      hidden : true,
		      editor : {
			     xtype : 'textfield',
			     maxLength : 50
		      }
		     }, {
		      dataIndex : 'key4',
		      hidden : true,
		      editor : {
			     xtype : 'textfield',
			     maxLength : 50
		      }
		     }, {
		      dataIndex : 'key5',
		      hidden : true,
		      editor : {
			     xtype : 'textfield',
			     maxLength : 50
		      }
		     }, {
		      dataIndex : 'data1',
		      hidden : true,
		      editor : {
			     xtype : 'textfield',
			     maxLength : 100
		      }
		     }, {
		      dataIndex : 'data2',
		      hidden : true,
		      editor : {
			     xtype : 'textfield',
			     maxLength : 100
		      }
		     }, {
		      dataIndex : 'data3',
		      hidden : true,
		      editor : {
			     xtype : 'textfield',
			     maxLength : 100
		      }
		     }, {
		      dataIndex : 'data4',
		      hidden : true,
		      editor : {
			     xtype : 'textfield',
			     maxLength : 100
		      }
		     }, {
		      dataIndex : 'data5',
		      hidden : true,
		      editor : {
			     xtype : 'textfield',
			     maxLength : 100
		      }
		     }, {
		      text : T.label.createTime,
		      dataIndex : 'createDate',
		      xtype : 'datecolumn',
		      format : 'Y-m-d H:i:s',
		      width : 130
		     }, {
		      text : T.label.createUser,
		      dataIndex : 'createUserId'
		     }, {
		      text : T.label.updateTime,
		      dataIndex : 'updateDate',
		      xtype : 'datecolumn',
		      format : 'Y-m-d H:i:s',
		      width : 130
		     }, {
		      text : T.label.updateUser,
		      dataIndex : 'updateUserId'
		     }],
		   store : {
		    xtype : 'json',
		    fields : ['orgCode', 'groupName', 'groupDesc', 'key1', 'key2', 'key3', 'key4', 'key5', 'data1', 'data2',
		      'data3', 'data4', 'data5', {
			     type : 'date',
			     name : 'createDate',
			     dateFormat : 'YmdHis'
		      }, 'createUserId', {
			     type : 'date',
			     name : 'updateDate',
			     dateFormat : 'YmdHis'
		      }, 'updateUserId'],
		    proxy : {
			   type : 'zbusrpc',
			   module : 'BasViewCommonDataSetupDetailService',
			   reader : {
			    root : 'cdsDtlList'
			   }
		    }
		   }
	    });

	  //查询
	  this.txtSearchGroupName = Ext.create('Ext.form.field.Text', {
		   fieldLabel : T.label.group,
		   labelWidth : 60,
		   anchor : '98%'
	    });
	  this.txtSearchGroupDesc = Ext.create('Ext.form.field.Text', {
		   fieldLabel : T.label.desc,
		   labelWidth : 60,
		   anchor : '98%'
	    });
	  this.frmSearch = Ext.create('Ext.form.Panel', {
		   title : T.label.search,
		   items : [this.txtSearchGroupName, this.txtSearchGroupDesc]
	    })
	  //定义信息维护
	  this.txtGroupName = Ext.create('Ext.form.field.Text', {
		   fieldLabel : T.label.group,
		   name : 'groupName',
		   allowBlank : false,
		   maxLength : 20,
		   labelWidth : 60,
		   anchor : '98%'
	    });
	  this.txtGroupDesc = Ext.create('Ext.form.field.Text', {
		   fieldLabel : T.label.desc,
		   name : 'groupDesc',
		   maxLength : 100,
		   labelWidth : 60,
		   anchor : '98%'
	    });
	  this.txtKey1 = Ext.create('Ext.form.field.Text', {
		   fieldLabel : T.label.key + "1",
		   name : 'key1',
		   allowBlank : false,
		   maxLength : 50,
		   labelWidth : 60,
		   anchor : '98%'
	    });
	  this.txtKey2 = Ext.create('Ext.form.field.Text', {
		   fieldLabel : T.label.key + "2",
		   name : 'key2',
		   maxLength : 50,
		   labelWidth : 60,
		   anchor : '98%'
	    });
	  this.txtKey3 = Ext.create('Ext.form.field.Text', {
		   fieldLabel : T.label.key + "3",
		   name : 'key3',
		   maxLength : 50,
		   labelWidth : 60,
		   anchor : '98%'
	    });
	  this.txtKey4 = Ext.create('Ext.form.field.Text', {
		   fieldLabel : T.label.key + "4",
		   name : 'key4',
		   maxLength : 50,
		   labelWidth : 60,
		   anchor : '98%'
	    });
	  this.txtKey5 = Ext.create('Ext.form.field.Text', {
		   fieldLabel : T.label.key + "5",
		   name : 'key5',
		   maxLength : 50,
		   labelWidth : 60,
		   anchor : '98%'
	    });
	  this.txtData1 = Ext.create('Ext.form.field.Text', {
		   fieldLabel : T.label.value + "1",
		   name : 'data1',
		   maxLength : 50,
		   labelWidth : 60,
		   anchor : '98%'
	    });
	  this.txtData2 = Ext.create('Ext.form.field.Text', {
		   fieldLabel : T.label.value + "2",
		   name : 'data2',
		   maxLength : 50,
		   labelWidth : 60,
		   anchor : '98%'
	    });
	  this.txtData3 = Ext.create('Ext.form.field.Text', {
		   fieldLabel : T.label.value + "3",
		   name : 'data3',
		   maxLength : 50,
		   labelWidth : 60,
		   anchor : '98%'
	    });
	  this.txtData4 = Ext.create('Ext.form.field.Text', {
		   fieldLabel : T.label.value + "4",
		   name : 'data4',
		   maxLength : 50,
		   labelWidth : 60,
		   anchor : '98%'
	    });
	  this.txtData5 = Ext.create('Ext.form.field.Text', {
		   fieldLabel : T.label.value + "5",
		   name : 'data5',
		   maxLength : 50,
		   labelWidth : 60,
		   anchor : '98%'
	    });
	  //定义操作按钮
	  this.btnSaveDef = Ext.create('Ext.button.Button', {
		   iconCls : 'icon-save',
		   itemId : 'btnSaveDef',
		   text : T.label.save,
		   scope : this,
		   handler : this.onBtnSaveDefClicked
	    });
	  this.btnDeleteDef = Ext.create('Ext.button.Button', {
		   iconCls : 'icon-delete',
		   itemId : 'btnDeleteDef',
		   text : T.label.del,
		   scope : this,
		   handler : this.onBtnDeleteDefClicked
	    });
	  this.btnSearchDef = Ext.create('Ext.button.Button', {
		   iconCls : 'icon-search',
		   itemId : 'btnSearchDef',
		   text : T.label.search,
		   scope : this,
		   handler : this.onBtnSearchDefClicked
	    });
	  this.btnResetDef = Ext.create('Ext.button.Button', {
		   iconCls : 'icon-clear',
		   itemId : 'btnResetDef',
		   text : T.label.reset,
		   scope : this,
		   handler : this.onBtnResetDefClicked
	    });
	  this.frmOperater = Ext.create('Ext.form.Panel', {
		   title : T.label.operate,
		   flex : 1,
		   items : [this.txtGroupName, this.txtGroupDesc, this.txtKey1, this.txtKey2, this.txtKey3, this.txtKey4,
		     this.txtKey5, this.txtData1, this.txtData2, this.txtData3, this.txtData4, this.txtData5],
		   bbar : [this.btnSaveDef, this.btnDeleteDef, this.btnSearchDef, this.btnResetDef]
	    });

	  this.items = [{
		   xtype : 'panel',
		   region : 'center',
		   layout : {
		    type : 'vbox',
		    align : 'stretch'
		   },
		   items : [this.gridDef, {
		      xtype : 'pagingtoolbar',
		      store : this.gridDef.getStore(),
		      displayInfo : true
		     }, this.gridDtl]
	    }, {
		   xtype : 'panel',
		   region : 'east',
		   width : 250,
		   split : true,
		   layout : {
		    type : 'vbox',
		    align : 'stretch'
		   },
		   items : [this.frmSearch, this.frmOperater]
	    }];

	  this.callParent();
	 },
	 onGridDefSelect : function(rowModel, record, index) {
	  var dtlCols = this.gridDtl.columns;
	  Ext.each(dtlCols, function(dtlCol) {
		   var dtlDataIndex = dtlCol.dataIndex;
		   if (Ext.String.startsWith(dtlDataIndex, 'key') || Ext.String.startsWith(dtlDataIndex, "data")) {
		    dtlCol.setVisible(!Ext.isEmpty(record.get(dtlDataIndex)));
		    dtlCol.setText(record.get(dtlDataIndex));
		   }
	    });

	  this.gridDtl.getStore().load({
		   params : {
		    orgCode : record.get('orgCode'),
		    groupName : record.get('groupName')
		   }
	    });

	  this.frmOperater.loadRecord(record);
	 },
	 onBtnAddDtlClicked : function(btn) {
	  var defRcds = this.gridDef.getSelectionModel().getSelection();
	  if (Ext.isEmpty(defRcds)) {
		 return;
	  }

	  this.gridDtl.getStore().insert(0, {
		   orgCode : defRcds[0].get('orgCode'),
		   groupName : defRcds[0].get('groupName'),
		   key1 : '',
		   key2 : '',
		   key3 : '',
		   key4 : '',
		   key5 : '',
		   data1 : '',
		   data2 : '',
		   data3 : '',
		   data4 : '',
		   data5 : ''
	    });
	 },
	 onBtnSaveDtlClicked : function(btn) {
	  var dtlStore = this.gridDtl.getStore();
	  var dtlList = [];
	  dtlStore.each(function(rcd) {
		   if (!Ext.isEmpty(rcd.get('key1'))) {
		    dtlList.push({
			     orgCode : rcd.get('orgCode'),
			     groupName : rcd.get('groupName'),
			     key1 : rcd.get('key1'),
			     key2 : rcd.get('key2'),
			     key3 : rcd.get('key3'),
			     key4 : rcd.get('key4'),
			     key5 : rcd.get('key5'),
			     data1 : rcd.get('data1'),
			     data2 : rcd.get('data2'),
			     data3 : rcd.get('data3'),
			     data4 : rcd.get('data4'),
			     data5 : rcd.get('data5')
		      });
		   }
	    });

	  if (dtlList.length == 0) {
		 return;
	  }

	  Ext.Msg.show({
		   title : T.label.save,
		   msg : T.msg.are_you_sure_to_save_data,
		   icon : Ext.Msg.QUESTION,
		   buttons : Ext.Msg.YESNO,
		   scope : this,
		   fn : function(btnId) {
		    if (btnId == 'yes') {
			   btn.disable();
			   this.saveDtl(btn, dtlList);
		    }
		   }
	    });
	 },
	 saveDtl : function(btn, dataList) {
	  YX.ZbusRpc.request({
		   module : 'BasSaveCommonDataSetupDetailService',
		   scope : this,
		   showSuccess : true,
		   params : {
		    cdsDtlList : dataList
		   },
		   callback : function() {
		    btn.enable();
		   },
		   success : function() {
		    this.gridDtl.getStore().reload();
		   }
	    });
	 },
	 onBtnDeleteDtlClicked : function(btn) {
	  var dtlRcds = this.gridDtl.getSelectionModel().getSelection();
	  if (!Ext.isEmpty(dtlRcds)) {
		 this.gridDtl.getStore().remove(dtlRcds);
	  }
	 },
	 onBtnSaveDefClicked : function(btn) {
	  if (!this.frmOperater.getForm().isValid()) {
		 return;
	  }

	  Ext.Msg.show({
		   title : T.label.save,
		   msg : T.msg.are_you_sure_to_save_data,
		   icon : Ext.Msg.QUESTION,
		   buttons : Ext.Msg.YESNO,
		   scope : this,
		   fn : function(btnId) {
		    if (btnId == 'yes') {
			   btn.disable();
			   this.saveDef(btn);
		    }
		   }
	    });
	 },
	 saveDef : function(btn) {
	  YX.ZbusRpc.request({
		   module : 'BasSaveCommonDataSetupDefinitionService',
		   scope : this,
		   showSuccess : true,
		   params : {
		    cdsDefList : [this.frmOperater.getValues()]
		   },
		   callback : function() {
		    btn.enable();
		   },
		   success : function() {
		    this.gridDef.getStore().reload();
		    if (this.gridDtl.getStore().getCount() > 0) {
			   this.gridDtl.getStore().removeAll();
		    }
		   }
	    });
	 },
	 onBtnDeleteDefClicked : function(btn) {
	  if (Ext.isEmpty(this.gridDef.getSelectionModel().getSelection())) {
		 return;
	  }

	  Ext.Msg.show({
		   title : T.label.save,
		   msg : T.msg.are_you_sure_to_save_data,
		   icon : Ext.Msg.QUESTION,
		   buttons : Ext.Msg.YESNO,
		   scope : this,
		   fn : function(btnId) {
		    if (btnId == 'yes') {
			   btn.disable();
			   this.deleteDef(btn);
		    }
		   }
	    });
	 },
	 deleteDef : function(btn) {
	  var rcds = this.gridDef.getSelectionModel().getSelection();
	  var defData = [];
	  Ext.each(rcds, function(rcd) {
		   defData.push({
		      orgCode : rcd.get('orgCode'),
		      groupName : rcd.get('groupName')
		     });
	    });

	  YX.ZbusRpc.request({
		   module : 'BasDeleteCommonDataSetupDefinitionService',
		   scope : this,
		   showSuccess : true,
		   params : {
		    cdsDefList : defData
		   },
		   callback : function() {
		    btn.enable();
		   },
		   success : function() {
		    this.gridDef.getStore().reload();
		    if (this.gridDtl.getStore().getCount() > 0) {
			   this.gridDtl.getStore().removeAll();
		    }
		    this.frmOperater.getForm().reset();
		   }
	    });
	 },
	 onBtnSearchDefClicked : function(btn) {
	  var defProxy = this.gridDef.getStore().getProxy();
	  defProxy.setExtraParam('groupName', this.txtSearchGroupName.getValue());
	  defProxy.setExtraParam('groupDesc', this.txtSearchGroupDesc.getValue());
	  this.gridDef.getStore().load();

	  if (this.gridDtl.getStore().getCount() > 0) {
		 this.gridDtl.getStore().removeAll();
	  }
	 },
	 onBtnResetDefClicked : function(btn) {
	  this.frmOperater.getForm().reset();
	 }
  });